package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityVipFightCoefficient;
import com.loongtech.bi.manager.system.VipFightCoefficientManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.List;

/**
 * 游戏参数配置
 * 
 * @author marin
 * @date 2015-09-29
 */
@Controller
@Scope("view")
public class VipFightCoefficientConfigAction {

    private EntityVipFightCoefficient entity;
	private List<EntityVipFightCoefficient> list;
	
	@Resource
	private VipFightCoefficientManager vipFightCoefficientManager;


	
	@Autowired
	public void initial() {
		entity = new EntityVipFightCoefficient();
		list = vipFightCoefficientManager.queryList();
	}

    /**
     * 增加
     */
    public void add() {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        if (validate(faceContext, entity)) {
            vipFightCoefficientManager.persist(entity);
            faceContext.addMessage(null, new FacesMessage("提示", "添加成功！"));
            initial();
        }
    }

    /**
     * 删除
     */
    public void delete(int id) {
        FacesContext faceContext = FacesContext.getCurrentInstance();
            vipFightCoefficientManager.removeById(id);
            faceContext.addMessage(null, new FacesMessage("提示", "删除成功！"));
            initial();
    }

    /**
     * 修改
     */
    public void modify(EntityVipFightCoefficient e) {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        if (validate(faceContext, e)) {
            vipFightCoefficientManager.merge(e);
            faceContext.addMessage(null, new FacesMessage("提示", "修改成功！"));
            initial();
        }
    }

    private boolean validate(FacesContext faceContext, EntityVipFightCoefficient entity) {
        if (null == entity.getVipLevel()) {
            faceContext.addMessage(null, new FacesMessage("提示", "分组名称不能为空！"));
            return false;
        }
        if (null == entity.getVipCoefficient()) {
            faceContext.addMessage(null, new FacesMessage("提示", "通知方式不能为空！"));
            return false;
        }
        return true;
    }

    public EntityVipFightCoefficient getEntity() {
        return entity;
    }

    public void setEntity(EntityVipFightCoefficient entity) {
        this.entity = entity;
    }

    public List<EntityVipFightCoefficient> getList() {
        return list;
    }

    public void setList(List<EntityVipFightCoefficient> list) {
        this.list = list;
    }
}
